package com.hisense.ovcloud.apicontrol.controller;

import com.hisense.ovcloud.apicontrol.dto.req.CheckReplay6Req;
import com.hisense.ovcloud.apicontrol.dto.req.EncryptReq;
import com.hisense.ovcloud.apicontrol.dto.req.TokenReq;
import com.hisense.ovcloud.apicontrol.dto.req.ValidateReq;
import com.hisense.ovcloud.apicontrol.dto.resp.DynamicKeyResp;
import com.hisense.ovcloud.apicontrol.dto.resp.EncryptRes;
import com.hisense.ovcloud.apicontrol.dto.resp.TokenRes;
import com.hisense.ovcloud.apicontrol.dto.resp.ValidateResp;
import com.hisense.ovcloud.apicontrol.errors.CommonRes;
import com.hisense.ovcloud.apicontrol.service.ApiControlService;
import com.hisense.ovcloud.apicontrol.service.GatewayMainService;
import com.hisense.ovcloud.apicontrol.service.SecurityService;
import com.hisense.ovcloud.common3.validator.BaseRouterConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.path;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

@Configuration
public class RoutingConfiguration extends BaseRouterConfiguration {

    private final GatewayMainService gatewayMainService;

    private final SecurityService securityService;

    private final ApiControlService apiControlService;

    public RoutingConfiguration(GatewayMainService gatewayMainService, SecurityService securityService, ApiControlService apiControlService) {
        this.gatewayMainService = gatewayMainService;
        this.securityService = securityService;
        this.apiControlService = apiControlService;
    }

    @Bean
    public RouterFunction<ServerResponse> warehouseHimitRoutes() {
        return RouterFunctions.nest(path("/control"),
                route()
                        .POST("/validate", req -> ServerResponse.ok().body(gatewayMainService.validate(req.bodyToMono(ValidateReq.class)), ValidateResp.class))
                        .POST("/encrypt", req -> ServerResponse.ok().body(gatewayMainService.encrypt(req.bodyToMono(EncryptReq.class)), EncryptRes.class))
                        .POST("/tokenCheck", req -> ServerResponse.ok().body(gatewayMainService.tokenCheck(req.bodyToMono(TokenReq.class)), TokenRes.class))
                        .build());
    }

    @Bean
    public RouterFunction<ServerResponse> hisecRouters() {
        return RouterFunctions.nest(path("/"),
                route().GET("/getDynamicKey", req -> ServerResponse.ok().body(securityService.getDynamicKey(), DynamicKeyResp.class))
                        .POST("/check_replay", req -> ServerResponse.ok().body(apiControlService.verifyMono(req.bodyToMono(CheckReplay6Req.class)), CommonRes.class))
                        .build());
    }
}

